import bus from "@/utils/bus";
const PUSHKEY = "__push"
import { dataHelper } from 'hi-ui'
import { HiDataSetContainer, HiPage } from "hi-ui";
import request from "@/utils/request";
import hiSetting from '@/hiSetting'
import { mapGetters } from "vuex";
export default {
  extends: HiPage,
  computed: {
    ...mapGetters(["userInfo"]),
    dialogHeight() {
      return document.body.clientHeight * 0.65;
    }
  },
  methods: {



    //查询权限
    askPermission(routeName) {
      const permission_routes = this.$store.getters.permission_routeNames
      return permission_routes[routeName] || false;

    },
    validate() {

    },
    //关闭标签
    close(toName) {
      let visitedViews = this.$store.state.tagsView.visitedViews
      let currentView;
      for (let i = 0; i < visitedViews.length; i++) {
        let view = visitedViews[i]
        if (view.path === this.$route.path)
          currentView = view
      }

      this.$root.$children[0].$children[0].$children[2].closeSelectedTag(currentView, toName);
      if (toName) {
        let views = this.$root.$children[0].$children[0].$children[3].$children
        for (let i = 0; i < views.length; i++) {
          let view = views[i]
          if (view.$options.name == toName)
            return view;
        }
      }
      return null;
      //this.$root.$tags.closeSelectedTag(currentView)
    },
    //流程里调用closepage,所以这里重写
    closepage() {
      this.close()
    },
    _beforeclose(tags, view) {
      if (this.$route.name == view.name)
        this.beforeclose(tags, view)

    },
    beforeclose(tags, view) {

    },
    __getCurrView() {
      let visitedViews = this.$store.state.tagsView.visitedViews;
      let currView = null;
      if (visitedViews) {
        for (let i = 0; i < visitedViews.length; i++) {
          if (visitedViews[i].path == this.$route.path && visitedViews[i].target == (this.$route.query.__target || "")) {
            currView = visitedViews[i];
            break;
          }
        }
      }
      return currView;
    },
    //打开新标签
    open(name, params, query, { title, target } = {}) {

      let queryT = {}
      Object.assign(queryT, query || {})
      if (title)
        queryT.__title = title;
      if (target) {
        target = target === '_blank' ? (new Date()).valueOf() : target
        queryT.__target = target;
      }


      this.$router.push({
        name,
        params,
        query: queryT
      })
    },
    //打开新标签
    openByPath(path, query, { title, target } = {}) {
      query = query || {};
      let queryT = {}
      Object.assign(queryT, query || {});

      if (title)
        queryT.__title = title;
      if (target) {
        target = target === '_blank' ? (new Date()).valueOf() : target
        queryT.__target = target;
      }
      this.$router.push({
        path,
        query: queryT
      })
    },
    getParam(name) {
      let cruuView = this.__getCurrView();
      if (!cruuView)
        return null;
      if (!cruuView.params)
        return null;
      if (!name)
        return cruuView.params
      return cruuView.params[name]
    },
    /*****************************************以下是业务功能*************************************************** */
    initDsContainer() {
      this.$dsContainer = new HiDataSetContainer({
        request: request,
        saveUrl: hiSetting.saveUrl,
        queryUrl: hiSetting.queryUrl,
        datasetUrl: hiSetting.datasetUrl,
        policyUrl: this.$HI.policyUrl,
      })
    },
    //创建保存助手
    createSaveHelper(modelFile, funcPath, options = {}) {
      funcPath = funcPath || this.$route.path
      let saveHelper = new dataHelper.saveHelper(modelFile, funcPath, { request: this.$HI.request, url: this.$HI.saveUrl, pn: this.$HI.projectName });
      return saveHelper;
    },
    //数据查询
    queryEntity(param, modelFile, funcPath, options = {}) {
      funcPath = funcPath || this.$route.path
      let method = options.method || 'post'
      let extParam = options.extParam || {}
      return dataHelper.queryHelper.query(param, modelFile, funcPath, { request: this.$HI.request, url: this.$HI.queryUrl, method, pn: this.$HI.projectName, extParam })
    },
    //创建数据集
    createDataSet(datasetCfg, masterDataset) {
      return this.$dsContainer.createDataSet(Object.assign({ funcPath: this.$route.path, context: this }, datasetCfg), masterDataset);
    },
    //页面组件初始化后
    afterPageInit() {
      this.$nextTick(() => {
        this._viewInit();
        this._flowInit();
        this.viewInit();
      })
    },
    viewInit() {
    },
    //视图初始化
    _viewInit() {

      if (!this.dataBridge)
        return;
      let me = this;
      for (let ds of this.dataBridge.keys()) {
        if (typeof (ds) == 'string') {
          let dsInstance = me[ds];
          this.dataBridge.set(dsInstance, this.dataBridge.get(ds))
          ds = dsInstance;
        }

        ds.on("beforesave", (store) => {
          if (store.cancel)
            return;
          let cmpNames = this.dataBridge.get(store) || []
          if (!cmpNames)
            cmpNames = this.dataBridge.get(store.id)
          for (let i = 0; i < cmpNames.length; i++) {
            let cmp = this.$refs[cmpNames[i]]
            if (cmp && cmp.validate) {
              cmp.validate((result, rules) => {
                store.cancel = !result;
                if (!result) {
                  //提示网格的验证

                  if (cmp.$vnode.tag.endsWith("HiTable")) {
                    for (let i = 0; i < rules.length; i++) {
                      const element = rules[i];
                      let hasErr = false,
                        message = "";

                      for (let j = 0; j < element.length; j++) {
                        const error = element[j];
                        if (error.length > 0 && error[0].message) {
                          let errValidate = error[0]
                          hasErr = true;
                          message = message + errValidate.message + ",";
                        }
                      }
                      if (message.endsWith(",")) {
                        message = "第" + (i + 1) + "行" + message.substring(0, message.length - 1);
                        me.$message.error(message);
                      }
                      if (hasErr)
                        break;
                    }
                  }
                }
              })
              if (store.cancel)
                return;
            }
          }
        })
        ds.on("validateerror", function (store, rule) {
          me.$message.error(rule.result.message);
        });



      }
    },

    //流程初始化
    _flowInit() {
      let me = this;
      if (!this.flowConfig)
        return;

      let dataset = this.flowConfig.dataset;
      if (typeof (dataset) == 'string')
        dataset = this[dataset]
      dataset.on("afterload", function (response, data) {
        if (data.length > 0 && dataset.data[0].FAUDIT == 0) {
          dataset.setReadonly(true);
        }

      })
      dataset.on("aftersave", function (response, store, options) {
        if (store.data.length > 0 && store.data[0].FAUDIT == 0) {
          store.setReadonly(true);
        }
      })

    },
    userinfoInit() {
      this.userinfo = this.userInfo;
    }
  },
  mounted() {
    //this.initDsContainer()
    this.userinfoInit();
    bus.$on("beforetagclose", this._beforeclose);

  },
  destroyed() {
    bus.$off("beforetagclose", this.dirtychange);
  }
}